#!/usr/bin/php
<?php
   /**
    * Advert Cron processes.
    * Any questions 2 : romanshneer@gmail.com
    **/

function _advert_delete_node($nid)
{
$sql1="DELETE FROM advert_offers_node WHERE nid=".$nid;
db_query($sql1);

$sql2="DELETE FROM field_data_body WHERE entity_type='node' AND bundle='advert' AND entity_id=".$nid;
db_query($sql2);

$sql3="DELETE FROM field_revision_body WHERE entity_type='node' AND bundle='advert' AND entity_id=".$nid;
db_query($sql3);

$sql4="DELETE FROM node_advert_custom_fields WHERE nid=".$nid;
db_query($sql4);

$sql5="DELETE FROM node_advert WHERE nid=".$nid;
db_query($sql5);

$sql6="DELETE FROM node WHERE nid=".$nid;
db_query($sql6);

$sql7="DELETE FROM node_revision WHERE nid=".$nid;
db_query($sql7);

}

$cwd=getcwd();
$path=substr($cwd,0,strrpos($cwd,"/")+1);
#die();
define('DRUPAL_ROOT', $path);

include_once DRUPAL_ROOT . 'includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);

/* DELETE OLD ADVERTS */
$sql1="SELECT DISTINCT n.nid 
FROM node n
LEFT JOIN node_advert na ON na.nid=n.nid
LEFT JOIN node_advert_dates nad ON nad.nid=n.nid
WHERE n.type='advert' AND nad.public_date<'".date("Y-m-d",strtotime("-3 months"))."'
ORDER BY n.nid";
#print $sql1;
$n_rec=db_query($sql1);
while($n=$n_rec->fetchObject())
  {
    _advert_delete_node($n->nid);
  }
/*DELETE GABIGE DB :: UNUSABLE fields adverts and more */
$sql2="SELECT DISTINCT n.nid 
FROM node n 
LEFT JOIN `node_advert_dates` nad ON nad.nid=n.nid
WHERE n.type='advert' AND nad.public_date IS NULL";
$n2_rec=db_query($sql2);
while($n2=$n2_rec->fetchObject())
  {
    _advert_delete_node($n2->nid);
  }
?>